package com.grasp.activity.area;

import java.util.List;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.grasp.activity.BaseFragment;
import com.grasp.activity.R;
import com.grasp.base.CustomGridViewAdapter;
import com.grasp.data.IconItem;
import com.grasp.db.AreaTable;
import com.grasp.util.IntentUtil;

public class AreaMainFragment extends BaseFragment implements
		OnItemClickListener, OnItemLongClickListener {
	private CustomGridViewAdapter mAdapter;
	private GridView mGridView;
	private List<IconItem> items;
	private AreaTable mAreaTable;
	private PopupWindow mPopupWindow;

	@Override
	protected void initView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.activity_main_area, container, false);
		mGridView = (GridView) mView.findViewById(R.id.area_gridview);
	}

	@Override
	protected void main() {
		mAreaTable = new AreaTable(getActivity());
		items = mAreaTable.queryAll();
		IconItem item = new IconItem();
		item.setTitle("添加区域");
		item.setFileName("icon_add");
		items.add(item);
		mAdapter = new CustomGridViewAdapter(getActivity(), items);
		mGridView.setAdapter(mAdapter);

		mGridView.setOnItemClickListener(this);
		mGridView.setOnItemLongClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg2 == items.size() - 1) {
			IntentUtil.startActivity(getActivity(), AddAreaActivity.class);
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		if (arg2 != items.size() - 1) {
			showPopupWindow(arg1, arg2);
		}
		return false;
	}

	@Override
	public void onResume() {
		super.onResume();
		List<IconItem> mitems = mAreaTable.queryAll();
		items.clear();
		IconItem item = new IconItem();
		item.setTitle("添加区域");
		item.setFileName("icon_add");
		mitems.add(item);
		items.addAll(mitems);
		mAdapter.notifyDataSetChanged();
	}

	private void showPopupWindow(View mView, final int position) {
		View popupView = getActivity().getLayoutInflater().inflate(
				R.layout.popupwindow_layout, null);
		mPopupWindow = new PopupWindow(popupView,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, true);
		mPopupWindow.setAnimationStyle(R.style.PopupWindowAnim);
		mPopupWindow.setBackgroundDrawable(getActivity().getResources()
				.getDrawable(R.drawable.popupwindow_bg));
		mPopupWindow.setOutsideTouchable(false);
		mPopupWindow.showAtLocation(mView, Gravity.CENTER, 0, 0);
		popupView.findViewById(R.id.pop_delete).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						IconItem item = items.get(position);
						if (mAreaTable.delete(item)) {
							items.remove(position);
							mAdapter.notifyDataSetChanged();
						} else {
							Toast.makeText(getActivity(), "删除错误",
									Toast.LENGTH_SHORT).show();
						}
						mPopupWindow.dismiss();
						mPopupWindow = null;
					}
				});
		popupView.findViewById(R.id.pop_update).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Bundle bundler = new Bundle();
						bundler.putSerializable("IconItemStr",
								items.get(position));
						IntentUtil.startActivity(getActivity(),
								AddAreaActivity.class, bundler);
						mPopupWindow.dismiss();
						mPopupWindow = null;
					}
				});
	}

}
